import cv2
from PySide6.QtGui import QImage
from PySide6.QtCore import Qt 


class ImageProcessor():

    def __init__(self):
        self.model = None

    def set_model(self,model):
        self.model = model

    def process_image(self, image_path):
        if not image_path or not self.model:
            return None
        frame = cv2.imread(image_path)
        result = self.model(frame)
        rendered_frame = result.render()[0]
        rgb_image = cv2.cvtColor(rendered_frame, cv2.COLOR_BGR2RGB)
        h, w, ch = rgb_image.shape
        bytes_per_line = ch * w
        conver_to_qt_format = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
        p = conver_to_qt_format.scaled(640, 480, Qt.KeepAspectRatio)
        return p